 <template>
  <div>
    <el-button type="primary" @click="openRoleDia()">新增</el-button>
    <!-- 列表展示 -->
    <el-table :data="roleList" border style="width: 100%" class="role-list">
      <el-table-column prop="name" label="角色编码">
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="nameZh" label="角色名"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="openRoleDia(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="mini"
            @click="openResourceDia(scope.row.id)"
            >分配资源</el-button
          >
          <el-button type="text" size="mini">分配菜单</el-button>
          <el-button type="danger" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改或新增角色对话框 -->
    <el-dialog
      :title="addOrUpdateTitle"
      :visible.sync="editRoleDia"
      width="30%"
    >
      <el-form label-position="right" label-width="80px" :model="roleInfo">
        <el-form-item label="角色编码">
          <el-input v-model="roleInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="角色名">
          <el-input v-model="roleInfo.nameZh"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editRoleDia = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 资源对话框 -->
    <el-dialog title="资源权限" :visible.sync="resourceDia" width="30%">
      <el-tree
        ref="resourceTree"
        :check-strictly="true"
        :props="props"
        :data="listOpt"
        show-checkbox
        :default-checked-keys="checked"
        node-key="id"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resourceDia = false">取 消</el-button>
        <el-button type="primary" @click="submitResource()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

  <script>
export default {
  data() {
    return {
      roleList: [],
      editRoleDia: false,
      currentRoleId: 0,
      addRoleDia: false,
      addOrUpdateTitle: "新增或修改角色标题",
      resourceDia: false,
      checked: [],
      listOpt: [],
      roleInfo: {
        name: "",
        nameZh: "",
      },
      props: {
        children: "children",
        label: "resourceName",
      },
    };
  },
  methods: {
    init() {
      this.getRequest("/api/role/admin/list").then((res) => {
        this.roleList = res.data;
      });
    },
    openRoleDia(id) {
      if (id) {
        this.currentRoleId = id;
        this.getRoleById(id);
        this.addOrUpdateTitle = "修改角色";
      } else {
        this.addOrUpdateTitle = "新增角色";
        this.roleInfo = {
          name: "",
          nameZh: "",
        };
      }
      this.editRoleDia = true;
    },
    getRoleById(id) {
      this.getRequest("/api/role/admin/getRoleById?" + "id=" + id).then(
        (res) => {
          this.roleInfo = res.data;
        }
      );
    },
    editRole() {
      this.putRequest("/api/role/admin/edit", this.roleInfo).then((res) => {
        if (res.flag) {
          this.$message.success("修改成功");
          this.init();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    addRole() {
      this.postRequest("/api/role/admin/add", this.roleInfo).then((res) => {
        if (res.flag) {
          this.$message.success("新增成功");
          this.init();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    submit(type) {
      if (this.roleInfo.id) {
        // 修改
        this.editRole();
      } else {
        // 新增
        this.addRole();
      }
    },
    submitResource() {
        let resourceIds = this.$refs.resourceTree.getCheckedKeys();
        let params = {
          roleId:this.currentRoleId,
          ids:resourceIds
        }
        this.putRequest("/api/resource/admin/updateRoleResource",params).then(res=>{
          console.log(res)
          this.init();
        })
    },
    handleClose() {
      this.roleInfo = {
        name: "",
        nameZh: "",
      };
    },
    openResourceDia(id) {
      // 请求资源选项
      this.getRequest("/api/resource/admin/listOpt").then((res) => {
        this.listOpt = res.data;
      });
      // 请求已经拥有的资源id列表
      this.getRequest("/api/resource/admin/listByRoleId/" + id).then((res) => {
        this.checked = res.data;
      });
      this.currentRoleId=id;
      this.resourceDia = true;
    },
  },
  created() {
    this.init();
  },
};
</script>

<style>
.role-list {
  margin: 28px;
}
</style>